/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package triangulate;

import java.awt.Color;
import java.awt.Polygon;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author daniel
 */
public class ColoredPolygon extends Polygon {
    public Color color;
    public String originalStyle;

    String getSVGString()
        {
        System.out.println("SVG path:");
        int n;
        String toReturn = "";

        toReturn = "<path style='";
        toReturn += originalStyle;
        toReturn += "' d='";
        toReturn += " M ";
        toReturn += this.xpoints[ 0 ];
        toReturn += ",";
        toReturn += this.ypoints[ 0 ];

        for ( int c = 0; c < this.npoints; ++c ) {

            n = c % ( this.npoints );
            System.out.println("n:" + n + " of " + this.npoints );
            System.out.println(" " + this.xpoints[ n ] + "," + this.ypoints[ n ] );
            toReturn += " L ";
            toReturn += (  this.xpoints[ n ] );
            toReturn += ",";
            toReturn += ( this.ypoints[ n ] );
        }

        toReturn += " z' />\n";
        return toReturn;
        }



    public void writePath(FileOutputStream os)
        {
        byte[] bytes = new byte[ 10 ];
        bytes[ 0 ] = (byte) ( color.getAlpha() - 128 );
        bytes[ 1 ] = (byte) ( color.getRed() - 128 );
        bytes[ 2 ] = (byte) ( color.getGreen() - 128 );
        bytes[ 3 ] = (byte) ( color.getBlue() - 128 );


        bytes[ 4 ] = (byte) ( ( ( this.xpoints[ 0 ] * 255 ) / 800 ) - 128 );
        bytes[ 5 ] = (byte) ( ( ( this.ypoints[ 0 ] * 255 ) / 480 ) - 128 );

        bytes[ 6 ] = (byte) ( ( ( this.xpoints[ 1 ] * 255 ) / 800 ) - 128 );
        bytes[ 7 ] = (byte) ( ( ( this.ypoints[ 1 ] * 255 ) / 480 ) - 128 );

        bytes[ 8 ] = (byte) ( ( ( this.xpoints[ 2 ] * 255 ) / 800 ) - 128 );
        bytes[ 9 ] = (byte) ( ( ( this.ypoints[ 2 ] * 255 ) / 480 ) - 128 );

        try
            {
            os.write(bytes);
            }
        catch (IOException ex)
            {
            Logger.getLogger(ColoredPolygon.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
}
